package com.ibeetl.admin.console.service;

import com.ibeetl.admin.console.dao.BasUserConsoleDao;
import com.ibeetl.admin.core.entity.BasUser;
import com.ibeetl.admin.core.service.CoreBaseService;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.util.enums.GeneralStateEnum;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BasUserConsoleService extends CoreBaseService<BasUser> {



	@Autowired
	BasUserConsoleDao basUserDao;

	/**
	 * 根据条件查询
	 *
	 * @param query
	 */
	public void queryByCondtion(PageQuery<BasUser> query) {
		PageQuery<BasUser> ret = basUserDao.queryByCondtion(query);
		queryListAfter(ret.getList());
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 */
	public void batchDelSysUser(List<String> ids) {
		try {
			basUserDao.batchDelUserByIds(ids);
		} catch (Exception e) {
			throw new PlatformException("批量删除用户失败", e);
		}
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 */
	public void batchDelUser(List<Long> ids) {
		try {
			basUserDao.batchDelByIds(ids);
		} catch (Exception e) {
			throw new PlatformException("批量删除用户失败", e);
		}
	}

	/**
	 * 批量删除
	 *
	 * @param id
	 */
	public BasUser queryById(String id) {
		try {
			return basUserDao.queryById(id);
		} catch (Exception e) {
			throw new PlatformException("更新用户信息失败", e);
		}
	}


	/**
	 * 插入一条用户数据
	 *
	 * @param user
	 */
	public void saveUser(BasUser user) {
		BasUser basUser = user;
		basUser.setCreateDate(new Date());
		basUser.setUserState(GeneralStateEnum.ENABLE.getValue());
		basUser.setPassword(user.getPassword());
		basUserDao.insert(basUser, true);
	}


	/**
	 * 重置用户密码
	 *
	 * @param id
	 * @param password
	 */
	public int resetPassword(Long id, String password) {
		BasUser user = new BasUser();
		user.setId(id);
		user.setPassword(password);
		user.setUpdateTime(new Date());
		return basUserDao.updateTemplateById(user);
	}
}
